package com.paw.common.util;

import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

/**
 * @author Rubble
 * @date 2021/8/24
 **/
@Slf4j
public class BeanUtilTools {


  /**
   * 拷贝list 对象
   * @param srcList  源列表
   * @param klassT 目标class
   * @param <T> 目标 class
   * @param <S> 源 class
   * @return
   */
  public static <T,S> List<T> copyList(List<S> srcList,Class<T> klassT){
    List<T> dstList = new ArrayList<>();
    for (S src : srcList) {
      try {
        T target = klassT.newInstance();
        BeanUtils.copyProperties(src,target);
        dstList.add(target);
      } catch (InstantiationException e) {
        throw new RuntimeException("BeanCopyUtils copyList ex.",e);
      } catch (IllegalAccessException e) {
        throw new RuntimeException("BeanCopyUtils copyList ex.",e);
      }
    }
    return dstList;
  }

}
